package com.yuke.cloud.service.pmc.controller.frontend;

import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.pmc.dto.ProdAttributeDto;
import com.yuke.cloud.service.api.pmc.dto.ProdSpecInfoDto;
import com.yuke.cloud.service.api.wmc.dto.StorageDto;
import com.yuke.cloud.service.api.wmc.service.WmcStorageFeignApi;
import com.yuke.cloud.service.pmc.dto.ProdSpecInfoAndPriceDto;
import com.yuke.cloud.service.pmc.service.ProdMainAttributeService;
import com.yuke.cloud.service.pmc.service.ProdProductService;
import com.yuke.cloud.service.pmc.service.ProdSpecService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by zys on 2018/09/27.
*/
@Slf4j
@Api(value = "WEB - ProdProductController", tags = "处理ProdProduct相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/mall/prod/product")
public class MallProdProductController extends BaseController {
    @Resource
    private ProdProductService prodProductService;
    @Resource
    private ProdSpecService prodSpecService;
    @Resource
    private ProdMainAttributeService prodMainAttributeService;
    @Resource
    private WmcStorageFeignApi wmsStorageFeignApi;

    @GetMapping("/app/category/{categoryId}")
    @ApiOperation(httpMethod = "GET", value = "app商品分类页面")
    public Wrapper<List<ProdSpecInfoAndPriceDto>> getProdByCategoryId(@ApiParam(name = "categoryId", value = "商品类别Id") @PathVariable Long categoryId) {
        List<ProdSpecInfoAndPriceDto> prodProducts= prodProductService.getProdByCategoryId(categoryId);
        return WrapMapper.ok(prodProducts);
    }

    @GetMapping("/app/regionId/{regionId}")
    @ApiOperation(httpMethod = "GET", value = "app商品地域推荐页面")
    public Wrapper<List<ProdSpecInfoAndPriceDto>> getProdByRegionId(@ApiParam(name = "categoryId", value = "商品类别Id") @PathVariable Long regionId) {
        List<ProdSpecInfoAndPriceDto> prodProducts= prodProductService.getProdByRegionId(regionId);
        return WrapMapper.ok(prodProducts);
    }

    @GetMapping("/app/getProdByProductId")
    @ApiOperation(httpMethod = "GET", value = "app商品分类页面")
    public Wrapper<List<ProdSpecInfoDto>> getProdByProductId(@ApiParam(name = "productId", value = "产品ID") @RequestParam(defaultValue = "0") Long productId,
                                                         @ApiParam(name = "warehouseId", value = "仓库ID") @RequestParam(defaultValue = "0") Long warehouseId,
                                                             @ApiParam(name = "saleType", value = "仓库ID") @RequestParam(defaultValue = "2") Byte saleType) {
        if (productId==0 || warehouseId==0 || saleType == 0){
            return WrapMapper.error("参数错误！");
        }

        StorageDto storageDto = new StorageDto();
        storageDto.setProductId(productId);
        storageDto.setWarehouseId(warehouseId);
        storageDto.setSaleType(saleType);
        storageDto.setIsEnable(true);

        Wrapper<List<StorageDto>> wrapper = wmsStorageFeignApi.selectWmsSpecsByProductId(storageDto);

        if (wrapper.getCode() != 200 || wrapper.getResult() == null || wrapper.getResult().size() < 1){
            return WrapMapper.error("未找到商品信息");
        }
        List<StorageDto> storageDtoList = wrapper.getResult();
        List<Long> specIds = storageDtoList.stream().map(StorageDto::getSpecId).collect(Collectors.toList());
        System.out.println(specIds);
        List<ProdSpecInfoDto> specList = prodSpecService.getStorageSpecInfoBySpecIds(specIds);

        List<ProdAttributeDto> prodAttributes = prodMainAttributeService.getProdMainAttributes(productId,specIds);

        for (ProdSpecInfoDto spec : specList){
            List<ProdAttributeDto> specAttribute = new ArrayList<>();
            for (StorageDto item : storageDtoList){
                if (spec.getSpecId().equals(item.getSpecId())){
                    BeanUtils.copyProperties(item,spec);
                    spec.setStockCount(item.getNumber() - item.getLockNumber());
                    if (saleType.equals((byte)2)){
                        spec.setSalePrice(item.getStockPrice());
                    }else{
                        spec.setSalePrice(item.getMarketPrice());
                    }
                }
            }
            for (ProdAttributeDto attributeDto : prodAttributes){
                if (spec.getSpecId().equals(attributeDto.getSpecId())){
                    specAttribute.add(attributeDto);
                }
            }
            spec.setProdAttributes(specAttribute);
        }

        return WrapMapper.ok(specList);
    }
}
